from typing import Optional
from sqlalchemy.orm import Session
from mooc.crud.crud_base import CRUDBase
from mooc.models.admin import Admin
from mooc.schemas.admin import AdminCreate, AdminUpdate

class CRUDAdmin(CRUDBase[Admin, AdminCreate, AdminUpdate]):
    def get_by_username(self, db: Session, *, username: str) -> Optional[Admin]:
        return self.get_by_field(db, "username", username)

    def delete(self, db: Session, *, id: int) -> Admin:
        """重写删除方法，实现软删除"""
        obj = self.get(db, id=id)
        if obj:
            obj.is_delete = 1
            db.add(obj)
            db.commit()
            db.refresh(obj)
        return obj

admin = CRUDAdmin(Admin)

